$(function(){
    $('.login-panel input').keyup(function(event){
        if(event.keyCode ==13){
            doLogin();
        }
    });

    resetCaptcha();
    $('.captcha-img').click(function(){
        resetCaptcha();
    })
})

function doLogin(){
    var loginName = $('.login-panel input[name=j_username]').val();
    var pwd = $('.login-panel input[name=j_password]').val();
    var captchaCode = $('.login-panel input[name=captchaCode]').val();
    if(!loginName){
        layer.msg('用户名不得为空');
        return false;
    }
    if(!pwd){
        layer.msg('密码不得为空');
        return false;
    }
    if(!captchaCode){
        layer.msg('验证码不得为空');
        return false;
    }

    $.ajax({
        type:'post',
        url:ctx+'/j_spring_security_check',
        data:{'j_username':loginName,'j_password':pwd,'captchaCode':captchaCode},
        dataType:'json',
        success:function(resp){
            if(resp.code=='OK'){
                location.href = ctx + resp.data;
            }else {
                resetCaptcha();
                if('CAPTCHA_ERROR'==resp.code){
                    layer.msg('验证码错误');
                }else if('USER_NOT_EXISTS'==resp.code){
                    layer.msg('用户不存在');
                }else{
                    layer.msg('用户名或密码错误');
                }

            }
        },error:function (resp){
            console.log(resp);
        }
    })
}

function resetCaptcha(){
    $('.captcha-img').attr('src',ctx+'captchaImage?='+new Date().getTime());
}